package com.demo.Tools;

import org.apache.commons.codec.binary.Base64;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.file.Files;
import java.util.UUID;

public class FileManage {
    //保存文件
    public static String getSavePath(String dirname) throws FileNotFoundException, FileNotFoundException {
        // 项目路径
        File path = new File(ResourceUtils.getURL("classpath:").getPath());
        if (!path.exists())
            path = new File("");
        // 绝对路径=项目路径+自定义路径
        File upload = new File(path.getAbsolutePath(), dirname+"/");
        if (!upload.exists())
            upload.mkdirs();
        //System.err.println("path1::" + upload.getAbsolutePath());//path1是正常的,和src同级
        // 绝对路径=项目路径+打包的
//        String path2 = ClassUtils.getDefaultClassLoader().getResource("").getPath();
//        System.err.println("path2"+path2);//path2的中文是乱码,/target/classes/
        return upload.getAbsolutePath();
    }
    public static String getSuffix(String filename)//获取文件后缀
    {
        String suffix="";
        for(int i=filename.length()-1;i>=0;i--)
        {
            if(filename.charAt(i)=='.')
            {
                return suffix;
            }
            suffix=filename.charAt(i)+suffix;
        }
        return "";
    }

    public static void StraighctRead(String path) {//直接读取图片
        String image = readImagebyName(path);
        System.out.println(image);
    }

    public static String readImagebyName(String name)//通过image文件夹里的图片名读取图片
    {
        try {
            return ReadImage(getSavePath("static/image")+'\\'+name);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("没有这个文件");
            return "";
        }
    }
    public static String ReadImage(String path) {//base64编码传输图片
        byte[] fileByte = null;
        try {
            File file = new File(path);
            fileByte = Files.readAllBytes(file.toPath());
        } catch (IOException e) {
            System.out.println("没有这个文件"+path);
            return "";
        }
        return Base64.encodeBase64String(fileByte);
    }
    public static String DownloadPicture(MultipartFile picture,String pathname) throws IOException {//下载图片至本地
        String originalFileName = picture.getOriginalFilename();//获取原始图片的扩展名
        String suffix=getSuffix(originalFileName);
        if(!suffix.equals("jpg") && !suffix.equals("png") && !suffix.equals("gif") && !suffix.equals("bmp")&&!suffix.equals("jepg"))
        {
            return "";
        }
        String newFileName = UUID.randomUUID() + "."+suffix;
        String newFilePath = getSavePath(pathname)+"\\" + newFileName; //新文件的路径
        picture.transferTo(new File(newFilePath));//将传来的文件写入新建的文件
        return newFileName;
    }
    public static boolean DeleteFile(String pathname)//删除文件，1代表成功，0代表失败
    {
        System.out.println(pathname);
        return new File(pathname).delete();
    }
    public static boolean DeletePictureByname(String pathname)//删除文件，1代表成功，0代表失败
    {
        try {
            return DeleteFile(getSavePath("static/image")+"\\"+pathname);
        } catch (FileNotFoundException e) {
            return false;
        }
    }


}
